/**
 *@description:TODO
 * @author: cai
 *@time:2018年1月29日上午9:24:59
 */
package com.pos.modules.fast;

import android.content.Intent;
import android.content.res.Configuration;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.pos.BaseActivity;
import com.pos.R;
import com.pos.modules.cashier.AccountActivity;
import com.pos.net.base.Request.Method;
import com.pos.net.netparam.NetParams;
import com.pos.net.netparam.RequestUrls;
import com.pos.net.request.interfa.BaseRequestListener;
import com.pos.net.request.interfa.JsonRequestListener;
import com.pos.widgets.ActionBarManager;

import org.json.JSONObject;

/**
 * @description:快速收银
 * @author cai
 * @time:2018年1月29日上午9:24:59
 */
public class FastCashierActivity extends BaseActivity implements OnClickListener {
	private TextView text_num;
	private Button button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9, button_0,
			button_del, button_confirm;
	private String amount = "0.00";
	private long orderAmount = 0;
	private StringBuffer sb = new StringBuffer();

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void initView() {
		setContentView(R.layout.fast_cashier_layout);
		text_num = bindId(R.id.text_num);
		button_1 = bindId(R.id.button_1);
		button_2 = bindId(R.id.button_2);
		button_3 = bindId(R.id.button_3);
		button_4 = bindId(R.id.button_4);
		button_5 = bindId(R.id.button_5);
		button_6 = bindId(R.id.button_6);
		button_7 = bindId(R.id.button_7);
		button_8 = bindId(R.id.button_8);
		button_9 = bindId(R.id.button_9);
		button_0 = bindId(R.id.button_0);
		button_del = bindId(R.id.button_del);
		button_confirm = bindId(R.id.button_confirm);

	}

	@Override
	public void initData() {
		ActionBarManager.init(this, "快速收银", true, null, null);
		sb.setLength(0);
		sb.append("0.00");
	}

	@Override
	public void initListener() {
		button_del.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				sb.setLength(0);
				sb.append("0.00");
				amount = sb.toString();

				text_num.setText(amount);
				return true;
			}
		});

		button_1.setOnClickListener(this);
		button_2.setOnClickListener(this);
		button_3.setOnClickListener(this);
		button_4.setOnClickListener(this);
		button_5.setOnClickListener(this);
		button_6.setOnClickListener(this);
		button_7.setOnClickListener(this);
		button_8.setOnClickListener(this);
		button_9.setOnClickListener(this);
		button_0.setOnClickListener(this);
		button_del.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (amount.equals("0.00")) {
					return;
				}
				// 50.22
				sb.deleteCharAt(sb.length() - 3);
				// 5022
				sb.deleteCharAt(sb.length() - 1);
				// 502
				if (sb.length() < 3) {
					sb.insert(0, "0");
				}

				sb.insert(sb.length() - 2, ".");
				amount = sb.toString();
				text_num.setText(amount);
			}
		});

		button_confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (amount.equalsIgnoreCase("0.00")) {
					showShortToast("请输入大于0的金额");
					return;
				}
				if (Double.valueOf(amount) >= 10000000) {
					showShortToast("请输入小于1千万的金额");
					return;
				}

				netRequest.startRequest(RequestUrls.hostUrl + RequestUrls.posCreateOrderWithoutGoods, Method.POST,
						NetParams.posCreateOrderWithoutGoods(mContext, amount.replace(".", "")), 0, listener);

			}
		});

	}

	@Override
	public void onClick(View v) {
		
		if(sb.length()==10){
			showShortToast("请输入小于1千万的金额");
			return;
		}
		switch (v.getId()) {
		case R.id.button_1:
				sb.append("1");
			break;
		case R.id.button_2:
			sb.append("2");
			break;
		case R.id.button_3:
			sb.append("3");
			break;
		case R.id.button_4:
			sb.append("4");
			break;
		case R.id.button_5:
			sb.append("5");
			break;
		case R.id.button_6:
			sb.append("6");
			break;
		case R.id.button_7:
			sb.append("7");
			break;
		case R.id.button_8:
			sb.append("8");
			break;
		case R.id.button_9:
			sb.append("9");
			break;
		case R.id.button_0:
			if (!amount.equals("0.00")) {
				sb.append("0");
			}
			break;

		// case R.id.button_del:
		// if (sb.length() != 0) {
		// sb.deleteCharAt(sb.length() - 1);
		// }
		//
		// break;

		default:
			break;
		}
		if (sb.toString().equals("0.00")) {
			return;
		}
		// 0.004先删除小数点
		sb.deleteCharAt(sb.length() - 4);
		if (sb.toString().startsWith("0")) {
			sb.deleteCharAt(0);
		}
		// 011
		sb.insert(sb.length() - 2, ".");
		amount = sb.toString();
		text_num.setText(amount);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == 1) {
				sb.setLength(0);
				sb.append("0.00");
				amount = sb.toString();

				text_num.setText(amount);
			}

		}
	}

	private BaseRequestListener listener = new JsonRequestListener() {

		@Override
		public void onStart(int requestId) {

			showDialg();
		}

		@Override
		public void onError(int requestId, int errorCode, String errorMsg) {
			disMissDialog();

			dealError(errorCode, errorMsg);

		}

		@Override
		public void onSuccess(int requestId, String url, JSONObject jsonResult) {
			disMissDialog();
			if (requestId == 0) {
				int retCode = jsonResult.optInt("retCode", 0);
				if (retCode == 1) {
					showShortToast("创建订单失败");
					return;
				}

				Intent intent = new Intent(mContext, AccountActivity.class);
				intent.putExtra("orderId", jsonResult.optString("orderId"));
				intent.putExtra("finalPrice", jsonResult.optLong("orderAmount",0));
				intent.putExtra("memberPayToken", jsonResult.optString("memberPayToken"));
				intent.putExtra("isFastCashier", true);
                intent.putExtra("orderCode",jsonResult.optString("orderCode"));
				startActivityForResult(intent, 1);
			}

		}
	};

}
